package com.ryujung.binary_tree.leetcode_94;

import java.util.*;


/*
 * @lc app=leetcode.cn id=94 lang=java
 *
 * [94] 二叉树的中序遍历
 */

// @lc code=start

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {

    /**
     * 递归方式
     * 时间复杂度：O(n)
     * 空间复杂度：O(n)
     */
    public static List<Integer> inorderTraversal1(TreeNode root) {
        if (root == null) {
            return Collections.emptyList();
        }
        ArrayList<Integer> ans = new ArrayList<Integer>();
        List<Integer> leftList = root.left != null ? inorderTraversal(root.left) : Collections.emptyList();
        List<Integer> rightList = root.right != null ? inorderTraversal(root.right) : Collections.emptyList();

        ans.addAll(leftList);
        ans.add(root.val);
        ans.addAll(rightList);
        return ans;
    }

    /**
     * 遍历方式
     * 时间复杂度：O(n)
     * 空间复杂度：O(n)
     */
    public static List<Integer> inorderTraversal2(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        LinkedList<TreeNode> stack = new LinkedList<TreeNode>();

        while (root != null || !stack.isEmpty()) {
            // 将当前父节点的所有左子树自顶向下入栈
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            // 将最下面的子树从栈中弹出，开始遍历
            root = stack.pop();
            ans.add(root.val);
            root = root.right;
        }
        return ans;
    }

    /**
     * morris算法
     * 将根节点与左子树的最右的元素相连接
     * 时间复杂度：O(2n)
     * 空间复杂度：O(1)
     */
    public static List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        while (root != null) {
            if (root.left != null) {
                // 遍历顺序：从根节点想左一步，然就一直到最右子树
                TreeNode cur = root.left;
                while (cur.right != null && cur.right != root) {
                    cur = cur.right;
                }
                // 如果最右子树为空，则说明是第一次遍历，增加向根节点的指向
                if (cur.right == null) {
                    cur.right = root;
                    // 易错，连接后，应当继续遍历根节点的左子树，而不是前驱节点的左子树
                    // root = cur;
                    root = root.left;
                } else {
                    // 第二次遍历（标记过）
                    ans.add(root.val);
                    // 确保不会重复再次记录相同的结点
                    cur.right = null;
                    root = root.right;
                }
            } else {
                // 左数为空，则直接直接输出根节点，并向右遍历
                ans.add(root.val);
                root = root.right;
            }
        }

        return ans;
    }


    public static void main(String[] args) {
        TreeNode node3 = new TreeNode(3, null, null);
        TreeNode node2 = new TreeNode(2, node3, null);
        TreeNode root = new TreeNode(1, null, node2);
        List<Integer> ansList = inorderTraversal22(root);
        System.out.println(ansList);
        // expect: 1 ,3, 2
        System.out.println(preOrderTraversal(root));
    }

    // 递归
    public static List<Integer> inorderTraversal11(TreeNode root) {
        if (root == null) return Collections.EMPTY_LIST;

        List<Integer> result = new ArrayList<>();
        result.addAll(inorderTraversal(root.left));
        result.add(root.val);
        result.addAll(inorderTraversal(root.right));

        return result;
    }

    // 迭代
    public static List<Integer> inorderTraversal22(TreeNode root) {
        Deque<TreeNode> stack = new LinkedList<>();
        List<Integer> result = new ArrayList<>();

        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            result.add(root.val);
            root = root.right;
        }
        return result;
    }

    // 迭代前序遍历
    public static List<Integer> preOrderTraversal(TreeNode root) {
        Deque<TreeNode> stk = new LinkedList<>();
        List<Integer> res = new ArrayList<>();

        while (root != null || !stk.isEmpty()) {
            while (root != null) {
                res.add(root.val);
                stk.push(root.right);
                root = root.left;
            }
            root = stk.pop();
        }
        return res;
    }

}
// @lc code=end

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}